<?php
function news_head($db, &$headers, $translate) {
	if (!isset($_GET['id']))
		throw new Exception("Missing news ID.");
	// Get page tags and title for meta tag
	if ($_GET['type'] == 'reader')
		$data = $db->fetchRow("SELECT id,title,tags FROM reader WHERE id = " . $db->quote($_GET['id']));
	else
		$data = $db->fetchRow("SELECT id,title,tags FROM news WHERE id = " . $db->quote($_GET['id']) . " AND published = 1");
	if ($data == null)
		throw new Exception("Content is not available.");
	// Increase hit count but only once per session
	if ($_GET['type'] != 'reader') {
		$visitorNamespace = new Zend_Session_Namespace('gcmsvisitor');
		if (!isset($visitorNamespace->visited['news'][(int)$_GET['id']])) {
			$db->update('news', array('hits' => new Zend_Db_Expr('(hits+1)')), 'id = '.$db->quote($_GET['id']));
			$visitorNamespace->visited['news'][(int)$_GET['id']] = true;
		}
	}
	$headers->headTitle($data['title']);
	if (!empty($data['tags']))
		$headers->headMeta()->setName('keywords', $data['tags']);
}

function news_main($db, &$view, $translate) {
	require_once './libs/phpbb.php';
	global $config;
	if (!empty($config['forumdir']))
		$phpbb = new Zend_PHPBB3($config['forumdir']);
	if ($_GET['type'] == 'reader') {
		$data = $db->fetchRow("SELECT id,title,summary as content,source,tags,timestamp as pubdate, annotation FROM reader WHERE id = ".$db->quote($_GET['id']));
		$data['content'] .= " <a href=\"$data[source]\" target=\"_blank\">" . $translate->_("[Read More..]") . "</a>";
		$data['source'] = parse_url($data['source'], PHP_URL_HOST);
	}
	else {
		$data = $db->fetchRow("SELECT * FROM news WHERE id = ".$db->quote($_GET['id'])." AND published = 1");
		if (isset($phpbb) && $phpbb->get_approved($data['comments']))
			$data['comments'] = array('fid' => $config['forumid'], 'tid' => $data['comments'], 'count' => $phpbb->get_count($data['comments']));
	}
	$data['tags'] = tag_builder($data['tags']);
	$view->assign('data', $data);
	$view->display('news.tpl');
}
?>
